const CLASS_ID = Components.ID("{ecfe1d30-a5af-11de-8a39-0800200c9a66}");
const CLASS_NAME = "OM Kill Main";
const CONTRACT_ID = "@gmail.com/OrkutManager;1";

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;

function OMMainKiller() { }
OMMainKiller.prototype =
{
	Block:
		function (aContentLocation, path)
		{
			var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.orkutmanager.");
			if (!pref.getBoolPref("Run") || !pref.getBoolPref("MainKill.Run")) return Ci.nsIContentPolicy.ACCEPT;
			if (path == "/content/browser.xul") return Ci.nsIContentPolicy.ACCEPT;
			if (aContentLocation.spec.match(/^https?:\/\/\w{1,5}\.orkut\./i))
			{
				if (aContentLocation.spec.match(/\/RedirLogin/i) || aContentLocation.spec.match(/AlbumZoom/i))
					return Ci.nsIContentPolicy.ACCEPT;
				if (aContentLocation.spec.match(/\/Main#/i))
					return Ci.nsIContentPolicy.REJECT_SERVER;
			}
			return Ci.nsIContentPolicy.ACCEPT;
		},
	shouldLoad:
		function (aContentType, aContentLocation, aRequestOrigin, aInsecNode, aMimeType, aExtra)
		{
			if (!aRequestOrigin) aRequestOrigin = {};
			if (!aRequestOrigin.path) aRequestOrigin.path = "";
			return OMMainKiller.prototype.Block(aContentLocation, aRequestOrigin.path);
		},
	shouldProcess:
		function (aContentType, aContentLocation, aRequestOrigin, aInsecNode, aMimeType, aExtra)
		{
			return OMMainKiller.prototype.Block(aContentLocation, aRequestOrigin.path);
		},
	QueryInterface:
		function (aIID)
		{
			if (!aIID.equals(Ci.nsIContentPolicy) && !aIID.equals(Ci.nsISupports))
				throw Components.results.NS_ERROR_NO_INTERFACE;
			return this;
		}
};

var OMMainKillerFactory =
{
	createInstance: function (aOuter, aIID)
	{
		if (aOuter != null) throw Components.results.NS_ERROR_NO_AGGREGATION;
		return (new OMMainKiller()).QueryInterface(aIID);
	}
};

var OMMainKillerModule =
{
	registerSelf: function (aCompMgr, aFileSpec, aLocation, aType)
	{
		aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
		aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
		var catMan = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
		catMan.addCategoryEntry("content-policy", CONTRACT_ID, CONTRACT_ID, true, true);
	},

	unregisterSelf: function (aCompMgr, aLocation, aType)
	{
		aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
		aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);
		var catMan = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
		catMan.deleteCategoryEntry("content-policy", CONTRACT_ID, true);	
	},
  
	getClassObject: function (aCompMgr, aCID, aIID)
	{
		if (!aIID.equals(Components.interfaces.nsIFactory)) throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
		if (aCID.equals(CLASS_ID)) return OMMainKillerFactory;
		throw Components.results.NS_ERROR_NO_INTERFACE;
	},
	canUnload: function (aCompMgr) { return true; }
};

function NSGetModule(aCompMgr, aFileSpec) { return OMMainKillerModule; }
